package org.jeecg.model.basedata;

import com.google.common.collect.Lists;

import java.beans.Transient;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;

public class Area {
  private static final long serialVersionUID = 1L;
  private Area parent;
  private String parentIds;
  private String code;
  private String name;
  private String type;
  private List<Area> childList = Lists.newArrayList();
  protected String id;		// 编号
  public Area() {}
  
  public Area(String id)
  {
    this();
    this.id = id;
  }
  @NotNull
  public Area getParent() {
    return this.parent;
  }
  
  public void setParent(Area parent) {
    this.parent = parent;
  }
  
  @Length(min=1, max=255)
  public String getParentIds() {
    return this.parentIds;
  }
  
  public void setParentIds(String parentIds) {
    this.parentIds = parentIds;
  }
  
  @Length(min=1, max=100)
  public String getName() {
    return this.name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  @Length(min=1, max=1)
  public String getType() {
    return this.type;
  }
  
  public void setType(String type) {
    this.type = type;
  }
  
  @Length(min=0, max=100)
  public String getCode() {
    return this.code;
  }
  
  public void setCode(String code) {
    this.code = code;
  }

  public List<Area> getChildList() { return this.childList; }
  
  public void setChildList(List<Area> childList)
  {
    this.childList = childList;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  @Transient
  public static void sortList(List<Area> list, List<Area> sourcelist, String parentId) {
    for (int i = 0; i < sourcelist.size(); i++) {
      Area e = (Area)sourcelist.get(i);
      if ((e.getParent() != null) && (e.getParent().getId() != null) && 
        (e.getParent().getId().equals(parentId))) {
        list.add(e);
        
        for (int j = 0; j < sourcelist.size(); j++) {
          Area childe = (Area)sourcelist.get(j);
          if ((childe.getParent() != null) && (childe.getParent().getId() != null) && 
            (childe.getParent().getId().equals(e.getId()))) {
            sortList(list, sourcelist, e.getId());
            break;
          }
        }
      }
    }
  }
  
  @Transient
  public boolean isAdmin() {
    return isAdmin(this.id);
  }
  
  @Transient
  public static boolean isAdmin(String id) {
    return (id != null) && (id.equals("1"));
  }
}
